<?php

declare(strict_types=1);

namespace App\Controller\AdminApi;

use Dotenv\Validator;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\DeleteMapping;
use Hyperf\HttpServer\Annotation\GetMapping;
use Hyperf\HttpServer\Annotation\Middleware;
use Hyperf\HttpServer\Annotation\PostMapping;
use Hyperf\HttpServer\Contract\RequestInterface;
use Hyperf\HttpServer\Contract\ResponseInterface;
use Hyperf\Validation\Contract\ValidatorFactoryInterface;
use App\Exception\XZXValidationException;
use App\Service\ChannelMchConnService;
use App\Utils\CustomUtils;
use App\Utils\DateUtils;
use Psr\Container\ContainerInterface;
use Hyperf\Logger\LoggerFactory;
use App\Middleware\AdminJWTAuthMiddleware;

/**
 * Class ChannelMchConnController
 * 商户-通道关联
 *
 * @package App\Controller
 * @Controller(prefix="/admin/api/channelMchConn")
 * @Middleware(AdminJWTAuthMiddleware::class)
 */
class ChannelMchConnController extends AdminBaseController {

    /**
     * @Inject()
     * @var ChannelMchConnService
     */
    protected $ChannelMchConnService;

    /**
     * @var \Psr\Log\LoggerInterface
     */
    protected $logger;

    public function __construct (LoggerFactory $loggerFactory) {
        // 第一个参数对应日志的 name, 第二个参数对应 config/autoload/logger.php 内的 key
        $this->logger = $loggerFactory->get('log', 'default');
    }

    /**
     * 设置限额
     * @PostMapping(path = "setTradeLimit")
     *
     * @param RequestInterface $request
     *
     * @return array
     * @throws \Throwable
     */
    public function setTradeLimit (RequestInterface $request) {

        $this->_validOrFail($request->all(), [
            'mchNo'                => 'required',
            'channelNo'            => 'required',

            "amountMin"            => 'required|integer|min:1',
            "amountLimit"          => 'required|integer|min:1',
            "dayCountLimit"        => 'required|integer|min:0',
            "monthCountLimit"      => 'required|integer|min:0',
            "dayAmountLimit"       => 'required|integer|min:0',
            "monthAmountLimit"     => 'required|integer|min:0',
            "dayAmountThreshold"   => 'required|integer|min:1|max:100',
            "monthAmountThreshold" => 'required|integer|min:1|max:100',
        ]);

        $param = [
            'mchNo'                => $request->post('mchNo'),
            'channelNo'            => $request->post('channelNo'),
            'amountMin'            => $request->post('amountMin'),
            'amountLimit'          => $request->post('amountLimit'),
            'dayCountLimit'        => $request->post('dayCountLimit'),
            'monthCountLimit'      => $request->post('monthCountLimit'),
            'dayAmountLimit'       => $request->post('dayAmountLimit'),
            'monthAmountLimit'     => $request->post('monthAmountLimit'),
            'dayAmountThreshold'   => $request->post('dayAmountThreshold'),
            'monthAmountThreshold' => $request->post('monthAmountThreshold'),
        ];

        $ext = $this->request->getAttribute("ext");

        $param['createBy'] = $ext['user']['username'];
        $param['updateBy'] = $ext['user']['username'];

        return $this->ChannelMchConnService->setTradeLimit($param);
    }

    /**
     * 设置状态
     * @PostMapping(path = "setStatus")
     *
     * @param RequestInterface $request
     *
     * @return array
     * @throws \Exception
     */
    public function setStatus (RequestInterface $request) {

        $this->_validOrFail($request->all(), [
            'mchNo'     => 'required',
            'channelNo' => 'required',
            'status'    => 'required',
        ]);

        $param = [
            'mchNo'     => $request->post('mchNo'),
            'channelNo' => $request->post('channelNo'),
            'status'    => !!(int) $request->post('status'),
        ];

        $ext = $this->request->getAttribute("ext");

        $param['createBy'] = $ext['user']['username'];
        $param['updateBy'] = $ext['user']['username'];

        return $this->ChannelMchConnService->setStatus($param);
    }

    /**
     * 获取通道限额消耗情况
     * @PostMapping(path = "getTradeLimitCost")
     *
     * @param RequestInterface $request
     *
     * @return array
     * @throws \Exception
     */
    public function getTradeLimitCost (RequestInterface $request) {

        $this->_validOrFail($request->all(), [
            'mchNo'     => 'required',
            'channelNo' => 'required',
        ]);

        $params = [
            'mchNo'     => $request->post('mchNo'),
            'channelNo' => $request->post('channelNo'),
        ];

        return $this->ChannelMchConnService->getTradeLimitCost($params);
    }

    /**
     * 获取通道限额使用情况列表
     * @PostMapping(path = "getTradeLimitCostList")
     *
     * @param RequestInterface $request
     *
     * @return array
     * @throws \Exception
     */
    public function getTradeLimitCostList (RequestInterface $request) {

        $this->_validOrFail($request->all(), [
            // 'mchNo'     => 'required',
        ]);

        $params = [
            'page'  => $request->post('page', null),
            'limit' => $request->post('limit', null),
        ];

        $search = $request->post('search', []);

        CustomUtils::xzxAddSearch($params, $search, 'mchNo');

        return $this->ChannelMchConnService->getTradeLimitCostList($params);
    }


}
